package com.banksoft.cps.service.impl;

import com.banksoft.cps.bean.*;
import com.banksoft.cps.bean.common.ServiceException;
import com.banksoft.cps.bean.query.CompanyQuery;
import com.banksoft.cps.bean.query.SupplierOrderQuery;
import com.banksoft.cps.bean.warpper.SupplierOrderWarpper;
import com.banksoft.cps.constants.ErrorConstant;
import com.banksoft.cps.dao.*;
import com.banksoft.cps.service.SupplierOrderService;
import com.banksoft.cps.util.DateUtil;
import com.banksoft.cps.util.UUIDFactory;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Created by rongzhao.zhu on 2018/1/12.
 */
@Service
public class SupplierOrderServiceImpl implements SupplierOrderService {
    @Autowired
    SupplierOrderDao supplierOrderDao;
    @Autowired
    CompanyDao companyDao;
    @Autowired
    SupplierCompanyDao supplierCompanyDao;
    @Autowired
    MaterialDao materialDao;

    @Autowired
    TransportCompanyDao transportCompanyDao;

    @Autowired
    SupplierIntoWarehouseDao supplierIntoWarehouseDao;

    @Override
    public List<SupplierOrder> list(SupplierOrderQuery query) {
        return supplierOrderDao.list(query);
    }

    @Override
    public List<SupplierOrderWarpper> listWarpper(SupplierOrderQuery query) {
        supplierQueryPreHandle(query);
        List<SupplierOrder> supplierOrders = supplierOrderDao.list(query);
        if(!supplierOrders.isEmpty() ){
            List<String> companyIdList = new ArrayList<>();
            List<String> supplierCompanyIdList =new ArrayList<>();
            List<String> materialIdList = new ArrayList<>();
            supplierOrders.forEach(e->{
                companyIdList.add(e.getCompanyId());
                supplierCompanyIdList.add(e.getSupplierCompanyId());
                materialIdList.add(e.getMaterialId());
            });
            List<Material>   materials = materialDao.listByIdList(materialIdList);
            List<SupplierCompany>  supplierCompanies = supplierCompanyDao.listByIdList(supplierCompanyIdList);
            List<Company>  companies = companyDao.listByIdList(companyIdList);
            Map<String,Material> materialMap = materials.stream().collect(Collectors.toMap(e->e.getId(),e->e));
            Map<String,SupplierCompany> supplierCompanyMap = supplierCompanies.stream().
                    collect(Collectors.toMap(e->e.getId(),e->e));
            Map<String,Company> companyMap = companies.stream().collect(Collectors.toMap(e->e.getId(),e->e));

            return supplierOrders.stream().map(e->{
                SupplierOrderWarpper warpper = new SupplierOrderWarpper();
                BeanUtils.copyProperties(e,warpper);
                Company company = companyMap.get(e.getCompanyId());
                warpper.setCompany(company);
                Material  material = materialMap.get(e.getMaterialId());
                warpper.setMaterial(material);
                SupplierCompany supplierCompany = supplierCompanyMap.get(e.getSupplierCompanyId());
                warpper.setSupplierCompany(supplierCompany);
                TransportCompany transportCompany = transportCompanyDao.findById(warpper.getTransportCompanyId());
                warpper.setTransportCompany(transportCompany);
                return warpper;

            }).collect(Collectors.toList());
        }


        return null;
    }

    @Override
    public long count(SupplierOrderQuery query) {
        supplierQueryPreHandle(query);
        return supplierOrderDao.count(query);
    }

    @Override
    public void save(SupplierOrder bean) throws ServiceException {

        try{
            bean.setTradeDateL(DateUtil.getTime2(bean.getTradeDate()));
        }catch (Exception e){
            throw new ServiceException(ErrorConstant.ec_param_error,
                    ErrorConstant.em_date_error);
        }

        if(StringUtils.isNotBlank(bean.getId())){
            supplierOrderDao.update(bean);
        }else {
            bean.setId(UUIDFactory.random());
            bean.setCreateTime(System.currentTimeMillis());
            supplierOrderDao.insert(bean);

            //保存入库记录
            SupplierIntoWarehouse supplierIntoWarehouse = new SupplierIntoWarehouse();
            supplierIntoWarehouse.setId(UUIDFactory.random());
            supplierIntoWarehouse.setCompanyId(bean.getCompanyId());
            supplierIntoWarehouse.setSupplierOrderId(bean.getId());
            supplierIntoWarehouse.setAmount(bean.getAmount());
            supplierIntoWarehouse.setUnit(bean.getUnit());
            supplierIntoWarehouse.setSupplierEmployee(bean.getSupplierEmployee());
            supplierIntoWarehouse.setTelephone(bean.getTelephone());
            supplierIntoWarehouse.setCreateTime(System.currentTimeMillis());
            supplierIntoWarehouseDao.insert(supplierIntoWarehouse);
        }
    }

    @Override
    public SupplierOrder findById(String id) {
        return supplierOrderDao.findById(id);
    }

    private void supplierQueryPreHandle(SupplierOrderQuery query){
        CompanyQuery companyQuery = new CompanyQuery();
        companyQuery.setName(query.getCompanyName());
        List<Company> companies = companyDao.list(companyQuery);
        if(!companies.isEmpty()){
            query.setCompanyIdList(companies.stream().map(e->e.getId()).collect(Collectors.toList()));
        }else{
            query.setCompanyIdList(Stream.of("sbgs").collect(Collectors.toList()));
        }
    }
}
